<style lang="scss" scoped>
	.flex{
		display: flex;
		align-items: center;
	}
	.page{
		background: #f8f8f8;
	}
	.list-cont{
		.item{
			margin-top: 20rpx;
			background: #FFFFFF;
			.type{
				color: #4c4c4c;
				font-size: 30rpx;
				height: 65rpx;
				border-bottom: 1px solid #ececec;
				padding-left: 36rpx;
			}
			.prod-info{
				width: 680rpx;
				margin: 0 auto;
				padding: 20rpx 0;
				align-items: flex-start;
				border-bottom: 1px solid #ececec;
				.pic{
					image{
						width: 182rpx;
						height: 182rpx;
						border-radius: 10rpx;
						display: block;
					}
				}
				.info{
					margin-left: 20rpx;
					.name{
						color: #4c4c4c;
						font-size: 34rpx;
						margin-top: 8rpx;
						line-height: 44rpx;
						height: 88rpx;
						width: 475rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.price{
						color: #f63c3c;
						font-size: 28rpx;
						margin-top: 8rpx;
					}
					.review{
						text{
							color: #b2b2b2;
							font-size: 24rpx;
							margin-right: 20rpx;
						}
					}
				}
			}
			.btns{
				width: 680rpx;
				margin: 0 auto;
				justify-content: flex-end;
				padding: 26rpx 0;
				button{
					padding: 0 30rpx;
					height: 50rpx;
					text-align: center;
					border: 1px solid #b3b3b3;
					border-radius: 10rpx;
					margin-left: 16rpx;
					color: #4c4c4c;
					font-size: 30rpx;
				}
			}
		}
	}
	.m-no-data{
		image{
			width: 100%;
			height: 100%;
		}
	}
</style>
<template>
	<view class="page">
		<view class="list-cont">
			<view class="item" v-for="(item, index) in datalist" :key="index">
				<view class="type flex"><text>发布时间：{{item.create_time}}</text></view>
				<view class="prod-info flex" @tap="to_detail(item.id)">
					<view class="pic"><image :src="item.photos_text[0]"></image></view>
					<view class="info">
						<view class="price">¥{{item.buyout_price}}</view>
						<view class="review flex"><!-- <text>想买12</text> --><text>浏览{{item.click_num}}</text></view>
					</view>
				</view>
				<view class="btns flex">
					<button @tap="del(item.id)">删除</button>
					<button v-if="item.state == '1'" @tap="save_state(item.id, index, '0')">下架</button>
					<button v-else @tap="save_state(item.id, index, '1')">重新上架</button>
				</view>
			</view>

		</view>
		<view class="m-no-data" v-if="showNodata">
			<image src="../../static/images/no_data.png" mode="widthFix"></image>
		</view>
		<view class="m-full-loading" v-if="bIsShowLoading">
			<view class="m-loading">
				<image src="../../static/images/loading.gif" mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import henglang from '@/common/common.js';
	export default { 
		data() {
			return {
				datalist: [],	// 列表数据
				page: 1,
				totalPages: 1,
				loadtype: 'normal',	// 加载数据方式
				bIsShowLoading: false,
				showNodata:false,
				search_data:{}
			}
		},
		
		onLoad(){
			this.getList({page: 1});
		},
		methods:{
			back(){
				uni.navigateTo({
					url:'/pages/user/user_index'
				})
			},
			save_state(id, index, state){
				let that = this;
				if(state == '0') {
					uni.showActionSheet({
					    itemList: ['已卖出','不想卖了'],
					    success: function (res) {
							let tapIndex = res.tapIndex + 2;
					        that.save_state_ajax(id, index, tapIndex);
					    }
					});
				}else{
					that.save_state_ajax(id, index, state);
				}
			},
			save_state_ajax(id, index, state) {
				let that = this;
				that.bIsShowLoading = true;
				henglang.post('Pawn/save_state', {id: id, state: state}, false,function(res){
					let resdata = res.data;
					that.bIsShowLoading = false;
					if(resdata.code == 'ok') {
						that.datalist[index].state = state;
					}else{
						henglang.showToast(resdata.msg, 1500);
					}
				})
			},
			to_detail(id){
				uni.navigateTo({
					url: '/pages/transfer/transfer_view?id=' + id
				})
			},
			// 上拉加载
			onReachBottom(){
				let that = this
				that.loadtype = 'plus';
				that.page++;
				that.search_data.page = that.page;
				if(that.datalist.length === 0) return false
				that.getList(that.search_data);
				that.bIsShowLoading = false
			},
			getList(data){
				let that = this;
				if(that.page <= that.totalPages){
					that.bIsShowLoading = true;
					henglang.post('Pawn/publish_list', data, false,function(res){
						that.bIsShowLoading = false;
						let data = res.data;
						let count = data.count;
						if(count == 0){
							that.showNodata = true
							that.datalist = []
							return false
						}
						if(that.loadtype == 'plus') {
							that.datalist = that.datalist.concat(data.data);
						}else{
							that.datalist = data.data;
							that.totalPages = data.total_pages;
							uni.stopPullDownRefresh();
						}
					})
				}else{
					return false
				}
			},
			del(id){
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '是否要删除',
				    success: function (res) {
				        if (res.confirm) {
				            henglang.get('Pawn/delPublish', {id:id}, false, function(res){
								if(res.data.code == 0){
									that.getList();
								}
				            })
				        }
				    }
				});
			}
		},
		onBackPress(options) {
		    if (options.from === 'navigateBack') {  
		        return false;  
		    }  
		    this.back();  
		    return true;  
		}
	}
	
</script>